﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace LinkClient_
{
    class Observer
    {
        InitFile init = new InitFile();
        MsSqlHelper mssql = new MsSqlHelper();

        public string GetEntName()
        {
            string info = "";
            ServiceReference.SaleApiSoapClient Soap = new ServiceReference.SaleApiSoapClient();
            DataSet ToDs = new DataSet();
            ServiceReference.MySoapHeader header = new ServiceReference.MySoapHeader();
            header.username = "scjrm";
            header.userpwd = "syncinfo";
            try
            {
                info=Soap.GetEntInfo(init.AppCode);
            }
            catch (Exception ex)
            {
                init.WriteLog(ex.ToString(), true);
                info = ex.Message.ToString();
            }
            return info;
        }

        public void GetSaleData()
        {
            string info = "";
            string sql = "";
            string infocode="";
            string infomessage="";
            ServiceReference.SaleApiSoapClient Soap = new ServiceReference.SaleApiSoapClient();
            DataSet ToDs = new DataSet();
            ServiceReference.MySoapHeader header = new ServiceReference.MySoapHeader();
            header.username = "scjrm";
            header.userpwd = "syncinfo";


            //DataTable dtdj = mssql.ExecuteDt("select max(djbh) as djbh from [jryy_djhz] where rq>=convert(varchar(10),getdate()-" + init.AppDay + ",120) order by djbh");
            DataTable dtdj = mssql.ExecuteDt("select max(djbh) as djbh from [jryy_djhz] order by djbh");
            DataTable dtsp = mssql.ExecuteDt("select max(spid) as spid from [jryy_sp] order by spid");
            dtdj.TableName = "dtdj";
            dtsp.TableName = "dtsp";
            ToDs.Tables.Add(dtdj);
            ToDs.Tables.Add(dtsp);

            DataSet ds = Soap.GetSaleData(header, init.AppCode, ToDs);

            if (ds != null)
            {
                /*
                foreach (DataTable dtt in ds.Tables)
                {
                    init.WriteLog("数据集："+dtt.TableName.ToString(), false);
                }
                 */
                if (ds.Tables.Contains("dsinfo"))
                {
                   // init.WriteLog("有数据集有表：dsinfo", false);
                    if (ds.Tables["dsinfo"].Rows.Count > 0)
                    {
                        infocode=ds.Tables["dsinfo"].Rows[0][0].ToString();
                        infomessage=ds.Tables["dsinfo"].Rows[0][1].ToString();
                        init.WriteLog(infocode+","+infomessage,false);
                    }
                }

                if (ds.Tables.Contains("dssp"))
                {                    
                    if (ds.Tables["dssp"].Rows.Count > 0)
                    {
                        init.WriteLog("开始更新【商品信息表】", false);
                        DataTable dssp = ds.Tables["dssp"];
                        for (int i = 0; i < dssp.Rows.Count; i++)
                        {
                            string spid = dssp.Rows[i]["spid"].ToString();
                            //init.WriteLog(spid, false);
                            if (!mssql.Getbool("select top 1 1 from [jryy_sp] where spid='" + spid + "'"))
                            {
                                string items = "";
                                for (int n = 0; n < dssp.Columns.Count; n++)
                                {
                                    items += "'" + dssp.Rows[i][n].ToString().Trim() + "',";
                                }
                                items = items.LastIndexOf(",") == items.Length - 1 ? items.Remove(items.Length - 1, 1) : items;

                                sql = "insert into [jryy_sp](spid,spbh,spmch,tongym,shpgg,dw,shengccj,sptm,jixing,pizhwh,leibie,wtscdw,zjm)values(" + items + ")";
                                mssql.ExecuteSql(sql);
                            }
                        }
                        init.WriteLog("结束更新【商品信息表】", false);
                    }
                }

                if (ds.Tables.Contains("dshz"))
                {
                    if (ds.Tables["dshz"].Rows.Count > 0)
                    {
                        init.WriteLog("开始更新【采购商品汇总表】", false);
                        DataTable dshz = ds.Tables["dshz"];
                        for (int i = 0; i < dshz.Rows.Count; i++)
                        {
                            string items = "";
                            for (int n = 0; n < dshz.Columns.Count; n++)
                            {
                                items += "'" + dshz.Rows[i][n].ToString().Trim() + "',";
                            }
                            items = items.LastIndexOf(",") == items.Length - 1 ? items.Remove(items.Length - 1, 1) : items;

                            sql = "insert into [jryy_djhz](djbh,rq,dwbh,hsje,thfs)values(" + items + ")";
                            mssql.ExecuteSql(sql);
                        }
                        init.WriteLog("结束更新【采购商品汇总表】", false);
                    }
                }

                if (ds.Tables.Contains("dsmx"))
                {
                    if (ds.Tables["dsmx"].Rows.Count > 0)
                    {
                        init.WriteLog("开始更新【采购商品明细表】", false);
                        DataTable dsmx = ds.Tables["dsmx"];
                        for (int i = 0; i < dsmx.Rows.Count; i++)
                        {
                            string items = "";
                            for (int n = 0; n < dsmx.Columns.Count; n++)
                            {
                                items += "'" + dsmx.Rows[i][n].ToString().Trim() + "',";
                            }
                            items = items.LastIndexOf(",") == items.Length - 1 ? items.Remove(items.Length - 1, 1) : items;
                            sql = "insert into [jryy_djmx](djbh,spid,pihao,sxrq,baozhiqi,dw, shl, hshj,hsje)values(" + items + ")";
                            mssql.ExecuteSql(sql);
                        }
                        init.WriteLog("结束更新【采购商品明细表】", false);
                    }
                }
            }            
        }
    }
}
